const router = require('koa-router')()
const {
	createBlog,
	queryBlog
} = require('../db/function/blogDB.js')

router.prefix('/blog')


// 新建博客
router.post('/createBlog', async (ctx, next) => {
	const {
		title,
		content,
		tag
	} = ctx.request.body
	if (title && content) {
		if (ctx.state) {
			const author = ctx.state.user.user
			const userId = ctx.state.user.id
			let res = await createBlog(userId,title, content, author, tag)
			console.log(res);
			if (res.affectedRows == 1) {
				ctx.succ('添加成功')
			} else {
				ctx.fail('添加失败')
			}

		} else {
			ctx.fail('token失效')
		}
	} else {
		ctx.fail('请输入必填信息')
	}


})

// 获取该用户博客
router.post('/getBlogs', async (ctx, next) => {
	console.log(ctx.state.user.id);
	const tags = ctx.request.body.tags.split(',')
	console.log(tags);
	
	const userId = ctx.state.user.id
	let res = await queryBlog(userId,tags)
	ctx.succ(res)
})



module.exports = router
